import Taro from '@tarojs/taro'
import {
  call, put, all, takeLatest
} from 'redux-saga/effects'
import { ADD_COMMENT } from '@ACTIONS/comment'
import api from '@API'

const addComment = function* addComment(action) {
  try {
    const { params } = action
    const data = yield call(api.createRating, params)
    yield put({ type: ADD_COMMENT.SUCCESS, data })
    Taro.eventCenter.trigger('changeExisting')
    Taro.showModal({
      title: '提交成功',
      content: '审核通过后将在医生主页显示。',
      mask: true,
      confirmColor: '#47C690',
      cancelColor: '#595959',
      showCancel: false,
      success: async () => {
        await Taro.navigateBack()
      }
    })
  } catch (error) {
    yield put({ type: ADD_COMMENT.FAILURE, error })
  }
}

export default function* rootSaga() {
  yield all([
    takeLatest(ADD_COMMENT.REQUEST, addComment),
  ]);
}
